package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.activity.AlbumDetailActivity;
import com.example.myapplication.R;
import com.example.myapplication.entity.HotAlbums;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HotAlbumsAdapter extends RecyclerView.Adapter<HotAlbumsAdapter.HotAlbumsHolder>{
    private List<HotAlbums>hotAlbumsList;
    private Context context;
    private BaseOnclickListener baseOnclickListener;

    public HotAlbumsAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<HotAlbums>hotAlbumsList){
        this.hotAlbumsList = hotAlbumsList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public HotAlbumsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hotalbums,parent,false);
        return new HotAlbumsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HotAlbumsHolder holder, int position) {
        HotAlbums hotAlbums = hotAlbumsList.get(position);
        String result2 = new SimpleDateFormat("yyyy-MM-dd").format(new Date(hotAlbums.getPublishTime()));
        holder.albums_name.setText(hotAlbums.getName());
        holder.hot_al_time.setText(result2);
        holder.hot_al_size.setText(hotAlbums.getSize()+" 首");
        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        Glide.with(context).load(hotAlbums.getPicUrl())
                .centerCrop().apply(options).into(holder.albums_img);

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AlbumDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("hotAlbums",hotAlbums);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return hotAlbumsList == null ? 0 :hotAlbumsList.size();
    }


    class HotAlbumsHolder extends RecyclerView.ViewHolder {
        ImageView albums_img;
        TextView albums_name;
        TextView hot_al_size;
        TextView hot_al_time;
        View view;
        public HotAlbumsHolder(@NonNull View itemView) {
            super(itemView);
            albums_img = itemView.findViewById(R.id.albums_img);
            albums_name = itemView.findViewById(R.id.albums_name);
            hot_al_size = itemView.findViewById(R.id.hot_al_size);
            hot_al_time = itemView.findViewById(R.id.hot_al_time);
            view = itemView.findViewById(R.id.view);
        }
    }
}
